import { useState } from "react";
import { useNavigate } from "react-router-dom";
import { motion } from "framer-motion";
import { useAuth } from "@/hooks/useAuth";
import { cn } from "@/lib/utils";

export default function StudentLogin() {
  const [studentId, setStudentId] = useState("");
  const [password, setPassword] = useState("");
  const [error, setError] = useState("");
  const [isShaking, setIsShaking] = useState(false);
  const navigate = useNavigate();
  const { login } = useAuth();

  const handleSubmit = (e: React.FormEvent) => {
    e.preventDefault();
    
    // 简单验证逻辑
    if (!studentId.trim() || !password.trim()) {
      setError("请输入学号和密码");
      triggerShake();
      return;
    }

    // 模拟登录验证
    if (studentId === "student" && password === "123456") {
      login();
      navigate("/student/dashboard");
    } else {
      setError("学号或密码错误");
      triggerShake();
    }
  };

  const triggerShake = () => {
    setIsShaking(true);
    setTimeout(() => setIsShaking(false), 500);
  };

  return (
    <div className="flex flex-col items-center justify-center min-h-screen bg-gray-50 p-4">
      <motion.div
        initial={{ opacity: 0, y: -20 }}
        animate={{ opacity: 1, y: 0 }}
        className="w-full max-w-md"
      >
        <div className="bg-white p-8 rounded-lg shadow-lg">
          <h1 className="text-3xl font-bold text-[#2C3E50] mb-6 text-center">
            学生登录
          </h1>
          
          <motion.form 
            onSubmit={handleSubmit}
            animate={isShaking ? { x: [-5, 5, -5, 5, 0] } : {}}
            transition={{ duration: 0.3 }}
          >
            <div className="mb-4">
              <label htmlFor="studentId" className="block text-gray-700 mb-2">
                学号
              </label>
              <input
                id="studentId"
                type="text"
                value={studentId}
                onChange={(e) => setStudentId(e.target.value)}
                className={cn(
                  "w-full px-4 py-2 border rounded-lg focus:outline-none focus:ring-2",
                  error ? "focus:ring-[#E74C3C]" : "focus:ring-[#2C3E50]"
                )}
                placeholder="请输入学号"
              />
            </div>

            <div className="mb-6">
              <label htmlFor="password" className="block text-gray-700 mb-2">
                密码
              </label>
              <input
                id="password"
                type="password"
                value={password}
                onChange={(e) => setPassword(e.target.value)}
                className={cn(
                  "w-full px-4 py-2 border rounded-lg focus:outline-none focus:ring-2",
                  error ? "focus:ring-[#E74C3C]" : "focus:ring-[#2C3E50]"
                )}
                placeholder="请输入密码"
              />
            </div>

            {error && (
              <motion.p 
                initial={{ opacity: 0 }}
                animate={{ opacity: 1 }}
                className="text-[#E74C3C] mb-4 text-center"
              >
                {error}
              </motion.p>
            )}

            <button
              type="submit"
              className="w-full bg-[#2C3E50] hover:bg-[#1a2635] text-white font-bold py-2 px-4 rounded-lg transition-colors"
            >
              登录
            </button>
          </motion.form>
        </div>
      </motion.div>
    </div>
  );
}
